(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.glSystem.createRejectController', createRejectController);
	createRejectController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'glSystemService' ];
	function createRejectController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, glSystemService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.validatorConfig = validatorConfig;
		ctrl.glSystem = {};

		glSystemService.initPageCallback(ctrl);
		ctrl.init = function() {
			glSystemService.getGlSystemLayout('createReject').then(function(layoutRsponse) {
				if (!layoutRsponse.data) {
					return;
				}
				ctrl.layoutConfig = layoutRsponse.data;
				glSystemService.loadOptions().then(function(glSystemOptions) {
					var glSystem;
					glSystemService.initOptions(ctrl, glSystemOptions);
					authService.createReject(params).then(function(response) {
						glSystem = response.data.model;
						if (glSystem) {
							ctrl.intaccrmethodOption = glSystem.intaccrmethodList;
							ctrl.feeamortmethodOption = glSystem.feeamortMethodList;
							ctrl.pastduemethodOption = glSystem.pastdueMethodList;
							ctrl.glMgrOption = glSystem.glpostMgrList.data;
							ctrl.glSystem = glSystem;
				    		ctrl.glSystem.taskId = response.data.task.taskId;
				    	}
					});
				});
			});
		};
		ctrl.createRejectSave = function() {
			return authService.createRejectSave(ctrl.glSystem).then(saveSuccessful, saveFailed);
		};

		ctrl.createRejectSubmit = function() {
			return authService.createRejectSubmit(ctrl.glSystem).then(submitSuccessful, submitFailed);
		};
		
		ctrl.createRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.glSystem.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createRejectCancel(ctrl.glSystem).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.glSystem.list',
					'state' : 'cda.glSystem.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.glSystem = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.glSystem.list',
					'state' : 'cda.glSystem.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
